project(fnote)
cmake_minimum_required(VERSION 2.6)

# Force out-of-source build
include(cmake/ForceOutOfSourceBuildTree.cmake)

include(cmake/FindMercurial.cmake)

# Set build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build, options are: Debug Release." FORCE)
endif(NOT CMAKE_BUILD_TYPE)

# configure
find_package(Qt4 REQUIRED)

if(NOT QT_QTSQL_FOUND)
    message(FATAL_ERROR "QtSql is missing.")
endif(NOT QT_QTSQL_FOUND)
set(QT_USE_QTSQL 1)

if(NOT QT_QTSVG_FOUND)
    message(FATAL_ERROR "QtSvg is missing.")
endif(NOT QT_QTSVG_FOUND)
set(QT_USE_QTSVG 1)

include(${QT_USE_FILE})

## fnote-config.h
# version
file(
    READ ${CMAKE_SOURCE_DIR}/VERSION
    FNOTE_BASE_VERSION
)
string(
    REPLACE "\n"
    ""
    FNOTE_BASE_VERSION
    ${FNOTE_BASE_VERSION}
)
set(FNOTE_VERSION ${FNOTE_BASE_VERSION})
#set(FNOTE_CODENAME "")
if(Mercurial_FOUND)
    execute_process(
        COMMAND ${Mercurial_HG_EXECUTABLE} identify -n
        OUTPUT_VARIABLE FNOTE_CHANGESET
        OUTPUT_STRIP_TRAILING_WHITESPACE
        ERROR_QUIET
    )
    if(FNOTE_CHANGESET)
        set(FNOTE_VERSION "${FNOTE_VERSION} hg:${FNOTE_CHANGESET}")
    endif(FNOTE_CHANGESET)
endif(Mercurial_FOUND)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(DEBUG 1)
    set(FNOTE_VERSION "${FNOTE_VERSION}-debug")
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -ggdb")
else(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
    add_definitions(-DQT_NO_DEBUG -DNDEBUG -DQT_NO_DEBUG_OUTPUT)
endif(CMAKE_BUILD_TYPE STREQUAL "Debug")

# build
configure_file(${CMAKE_SOURCE_DIR}/src/fnote-config.h.in ${CMAKE_BINARY_DIR}/fnote-config.h)
configure_file(${CMAKE_SOURCE_DIR}/cmake_uninstall.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake IMMEDIATE @ONLY)
add_custom_target(uninstall
    "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
    VERBATIM
)
add_custom_target(run
    "${CMAKE_BUILD_TOOL}" "install"
    COMMAND "${CMAKE_INSTALL_PREFIX}/bin/fnote"
    DEPENDS "fnote" 
    VERBATIM
    COMMENT "Running program..."
)

# mark to build
include(cmake/BuildMarker.cmake)

add_subdirectory(src)

# install docs
install(
    FILES
        AUTHORS
        COPYING
        README
        INSTALL
        ChangeLog
    DESTINATION
        "share/doc/fnote"
)

message(STATUS "*** FNote ${FNOTE_VERSION} in ${CMAKE_BUILD_TYPE} mode will be build.")
message(STATUS "*** install prefix: ${CMAKE_INSTALL_PREFIX}")
message(STATUS "*** Configuration complete. ***")
